package cn.kgc.ssmmodulardemo.controller;

import cn.kgc.ssmmodulardemo.entity.User;
import cn.kgc.ssmmodulardemo.entity.vo.AddUserVo;
import cn.kgc.ssmmodulardemo.service.UserService;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.HttpSessionRequiredException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
@Api(description = "用户管理")
@Controller
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    @ApiOperation(value = "用户登录",produces = "application/json",notes = "状态码：10000：登陆成功，10001：用户名或密码不正确")
    @RequestMapping(value = "/login",method = RequestMethod.POST,produces = "application/json")
    @ResponseBody
    public Object userLogin(@RequestParam("userCode")String userCode,
                            @RequestParam("userPassword")String userPassword,
                            HttpSession httpSession){
        JSONObject jsonObject=new JSONObject();
//
        String data="";
//
        String message="";
//
        String statCode="";
//
        User user=new User();
        user.setUserCode(userCode);
        user.setUserPassword(userPassword);
        User result=userService.userLogin(user);
        if(result!=null){
            data=result.toString();
            message="成功";
            statCode="10000";


        }else {
            data="";
            message="用户名密码错误";
            statCode="10001";


        }
        jsonObject.put("data",data);
        jsonObject.put("message",message);
        jsonObject.put("statCode",statCode);
        return jsonObject.toString();


    }

    /**
     * Vaild校验数据合法性
     * @param addUserVo 视图对象 要求该对象的属性内容必须和前端传过来的值一模一样
     * @param result 用来返回对象是否合法
     * @return 返回视图
     */
    @ApiOperation(value = "用户添加",produces = "application/json")
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public ModelAndView testAdd(@RequestBody AddUserVo addUserVo, BindingResult result){
        ModelAndView modelAndView=new ModelAndView();
        if (result.hasErrors()){
            System.out.println("检测到数据的不合法性，数据如下");
            System.out.println(result.getFieldErrors().toString());
            modelAndView.setViewName("error");
        }else{
            modelAndView.setViewName("success");
        }
        return new ModelAndView();
    }
    @ApiOperation(value = "swagger测试",produces = "application/json")
    @RequestMapping(value = "swaggerTest",method = RequestMethod.POST,produces = "application/json")
    @ResponseBody
    public Object swaggerTest(@RequestBody AddUserVo addUserVo){
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("message","访问成功");
        jsonObject.put("data",addUserVo.toString());
        return  jsonObject.toString();

    }
}
